package net.zhengxinyun.performance.App.appCtrl;

import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.performance.App.dto.IncentiveDTO;
import net.zhengxinyun.performance.App.dto.ScoreDTO;
import net.zhengxinyun.performance.Util.DateUtils;
import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.entity.DepartmentEntity;
import net.zhengxinyun.performance.entity.SetTimeEntity;
import net.zhengxinyun.performance.filter.*;
import net.zhengxinyun.performance.service.department.DepartmentService;
import net.zhengxinyun.performance.service.login.UserService;
import net.zhengxinyun.performance.service.setTime.SetTimeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public abstract class BaseCtrl {

    protected Logger logger  = LoggerFactory.getLogger(BaseCtrl.class);

    @Autowired
    private UserService userService;
    @Autowired
    private SetTimeService setTimeService;
    @Autowired
    private DepartmentService departmentService;



    /**
     * 获取手机号 用户信息
     * @param
     * @return
     */
    public  UserFilter getUserInfo(String tel)
    {
        UserFilter filter = new UserFilter();
        filter.setUserPhone(tel);
        List<UserFilter> userFilters = userService.queryUsers(filter);
        if(ObjectUtils.isNotBlank(userFilters))
        {
            return  userFilters.get(0);
        }

        return null;
    }


    //获取系统设置的当前年份。 控制取当前时间
    public String getCurrentYear(){

        SetTimeEntity currentYear = setTimeService.findByIndexClass("3");
        if(ObjectUtils.isNotBlank(currentYear))
        {
            return  currentYear.getStartTime();
        }
        return DateUtils.getCurrentYear();
    }

    //根据code 获取单位；
    public DepartmentEntity getDepartByCode(String departCode)
    {
        DepartmentEntity departmentEntity = departmentService.selectByCode(departCode);
        return  departmentEntity;
    }

    // 过滤市对县市指标(市领导的过滤)
    public  List<ScoreDTO> filterCityIndexs(List<CountryIndicatorsFilter> listIndesScore  , String leaderName)
    {

        List<ScoreDTO>  newIndisList = new ArrayList<>();

            //遍历处理可查看的指标  可查看的指标放在新的list中
           // openIndeds=userFilter.getIndexRight();
           // HashMap<String,Object> indexsMap = Util.getIndexIdMap(openIndeds);

            for (CountryIndicatorsFilter item : listIndesScore) {

                if (StringUtils.isNotBlank(item.getLeadName()) && item.getLeadName().contains(leaderName.trim())) {
                    newIndisList.add( new ScoreDTO( item.getId(), item.getFinalScore()==null?0:item.getFinalScore(), item.getScore()==null?0:item.getScore(), item.getWeight(), item.getRank(), null, item.getDepartcode(), item.getDateTime(), item.getDepartment(), item.getIndexName() ) );

                }
            }



        return newIndisList;
    }

    /**
     * 过滤县指标
     * @param list
     * @param user
     * @return
     */
    public List<TownIndicatorsFilter> filterTownIndex(List<TownIndicatorsFilter> list ,UserFilter user )
    {

        if("admin".equals(user.getUserPermissions()))
        {
            return  list;
        }
        List<TownIndicatorsFilter> indictorlist = new ArrayList<>();
        String indexRight = user.getIndexRight();
        if(StringUtils.isBlank(indexRight))
        {
            return  indictorlist;
        }
        HashMap<String, Object> indexIdMap = Util.getIndexIdMap(indexRight);
        for (TownIndicatorsFilter item : list) {

            Object idobj = indexIdMap.get(item.getId() + "");
            if (ObjectUtils.isBlank(idobj)) {
                continue;
            }
            String id1 = item.getId()+"";
            String id2 = indexIdMap.get(idobj+"")+"";
            if(id1.equals(id2))
            {
                indictorlist.add(item);
            }

        }

        return indictorlist;
    }



    //过滤正向激励
    public List<ScoreDTO> filetIncentive(List<IncentiveTypeFilter> listtypes, List<IncentiveDTO> incentiveDTOS ,String year ){

        Map<Long, Double> collect = incentiveDTOS.stream().collect(Collectors.toMap(o -> o.getTypeId(), o -> o.getSum()));
        List<ScoreDTO> scoreDTOS = new ArrayList<>();
       // double sumtype=0;
        for (IncentiveTypeFilter listtype : listtypes) {
            Double aDouble = collect.get(listtype.getId());
            if (ObjectUtils.isBlank(aDouble)) {
                scoreDTOS.add( new ScoreDTO(0,listtype.getTypeName(),listtype.getId(),year));
            }else {
                scoreDTOS.add(new ScoreDTO(aDouble.doubleValue(),listtype.getTypeName(),listtype.getId(),year));
               // sumtype= sumtype+aDouble.doubleValue();
            }

        }


        return  scoreDTOS;
    }

    public List<WorkScoreFilter> filterWorkIndex(List<WorkScoreFilter> query , String  userName){

        List<WorkScoreFilter> list = new ArrayList<>();
        for (WorkScoreFilter item : query) {

            if (userName.equals(item.getLeaderName())) {

                list.add(item);
            }

        }

        return list;

    }

}
